using System;
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;
using System.Windows.Forms;
using System.IO;

namespace InsertObject
{
    [WriterPlugin("fb3a1972-4489-4e52-abe7-25a00bb07fdf", "Insert Object",
        Description = "Allows you to insert an object to be uploaded to your blog",
        HasEditableOptions = false,
        Name = "Insert Object",
        PublisherUrl = "http://scottisafool.spaces.live.com/",
        ImagePath = "icon.gif")]
    [InsertableContentSource("File(s)")]

    public class InsertObjectPlugin : SmartContentSource
    {
        // If you change the name after class, please change it here too
        public InsertObjectPlugin()
        { }

        public override DialogResult CreateContent(IWin32Window dialogOwner, ISmartContent newContent)
        {
            
            PluginSettings settings = new PluginSettings(newContent.Properties);
            using (frmMain main = new frmMain(settings))
            {
                DialogResult result = main.ShowDialog();
                return result;
            }            
        }

        public override SmartContentEditor CreateEditor(ISmartContentEditorSite editorSite)
        {
            return new ContentEditor();
        }

        public override string GeneratePublishHtml(ISmartContent content, IPublishingContext publishingContext)
        {
            PluginSettings settings = new PluginSettings(content.Properties);
            string sn = publishingContext.ServiceName;
            if ((sn == "Windows Live Spaces" ||
                sn == "LiveJournal" ||
                sn == "Blogger") && !settings.UseFTP)
                return "Sorry, " + sn + " doesn't support file uploads<br>\n" +
                    "Please: <br>\n" + 
                    "1) Change to a different blog provider, then; <br> \n" +
                    "2) Save the draft <br>\n" +
                    "3) Change to HTML view, then back to the view you were on. <br>\n" +
                    "Your files will be ready to upload then.";
            string code = "";
            string label = settings.Label;
            string linktext = settings.LinkText;
            string fullpath = settings.Filename;
            string filename = Path.GetFileName(fullpath);
            string tempfilepath = "";
            string newwindow = "";
            try
            {
                content.Files.Add(filename, fullpath);
                tempfilepath = content.Files.GetUri(content.Files.Filenames[0]).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("File could not be added for the following reason: " + ex.Message, "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return "Inserting of file(" + filename + ") failed. Please try again.";
            }
            if (settings.NewWindow)
                newwindow = " target=\"_blank\"";
            if (!label.EndsWith(" "))
                label += " ";
            code = string.Format("<p>{0}<a href=\"{1}\"{2}>{3}</a>", label, tempfilepath, newwindow, linktext);
            if (settings.Filename2 != string.Empty)
            {
                string l = settings.Label2;
                string t = settings.LinkText2;
                string fp = settings.Filename2;
                string f2 = Path.GetFileName(fp);
                string tf = "";
                string w = "";
                try
                {
                    content.Files.Add(f2, fp);
                    tf = content.Files.GetUri(content.Files.Filenames[1]).ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("File could not be added for the following reason: " + ex.Message, "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return "Inserting of file(" + f2 + ") failed. Please try again.";
                }
                if (settings.NewWindow2)
                    w = " target=\"_blank\"";
                if (!l.EndsWith(" "))
                    l += " ";
                code += string.Format("<br />{0}<a href=\"{1}\"{2}>{3}</a>", l, tf, w, t);
            }
            if (settings.Filename3 != string.Empty)
            {
                string l = settings.Label3;
                string t = settings.LinkText3;
                string fp = settings.Filename3;
                string f2 = Path.GetFileName(fp);
                string tf = "";
                string w = "";
                try
                {
                    content.Files.Add(f2, fp);
                    tf = content.Files.GetUri(content.Files.Filenames[2]).ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("File could not be added for the following reason: " + ex.Message, "Big Fat Hairy Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return "Inserting of file(" + f2 + ") failed. Please try again.";
                }
                if (settings.NewWindow3)
                    w = " target=\"_blank\"";
                if (!l.EndsWith(" "))
                    l += " ";
                code += string.Format("<br />{0}<a href=\"{1}\"{2}>{3}</a>", l, tf, w, t);
            }
            code += "</p>";
            return code;
        }
        
    }
}
